package com.hzy.fivepalace.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzy.fivepalace.db.dao.DtsGroupProductMapper;
import com.hzy.fivepalace.db.domain.DtsGoods;
import com.hzy.fivepalace.db.domain.DtsGroupOpenList;
import com.hzy.fivepalace.db.domain.DtsGroupProduct;
import com.hzy.fivepalace.db.service.DtsGoodsService;
import com.hzy.fivepalace.db.service.DtsGroupOpenListService;
import com.hzy.fivepalace.db.service.DtsGroupProductService;
import com.hzy.fivepalace.db.service.base.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 拼团商品列Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-05
 */
@Service
public class DtsGroupProductServiceImpl extends BaseServiceImpl<DtsGroupProductMapper, DtsGroupProduct> implements DtsGroupProductService {

    @Resource
    private DtsGroupProductMapper groupProductMapper;
    @Resource
    private DtsGroupOpenListService groupOpenListService;
    @Resource
    private DtsGoodsService goodsService;

    @Override
    public DtsGroupProduct getGroupProduct(int groupId) {
        DtsGroupProduct groupProduct = groupProductMapper.selectById(groupId);
        if(groupProduct != null){
            DtsGoods goods = goodsService.selectById(groupProduct.getLinkId());
            if(goods!=null){
                groupProduct.setGoods(goods);
            }

            groupProduct.setIsHasGroup(0);
            QueryWrapper<DtsGroupOpenList> qwOpen = new QueryWrapper<>();
            qwOpen.eq("group_id", groupProduct.getId());
            qwOpen.eq("state", 0);
            qwOpen.last("limit 1");
            DtsGroupOpenList groupOpenList = groupOpenListService.getOne(qwOpen);
            if(groupOpenList != null){
                groupProduct.setIsHasGroup(1);
            }
            groupProduct.setGroupOpenList(groupOpenList);
        }
        return groupProduct;
    }
}
